<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title id="pageTitle"></title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <link rel="stylesheet" href="css/list.css">
    <script src="js/jquery.min.js"></script>
    <script src="./js/logout.js"></script>
    <script src="./js/common.js"></script>
    <script src="./js/islogin.js"></script>
    <script src="./js/urltools.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer" style="width: 24%;"></span>
        <div class="search">
            <input class="search-input" id="search-input" type="text" placeholder="输入文章标题">
            <button class="search-button" onclick="performSearch()">搜索</button>
        </div>

        <a href="blog_list.html">博客广场</a>
        <!-- 未登录显示元素 -->
        <span id="noneLoginElement">
            <a href="login.html">登录</a>
            <a href="reg.html">注册</a>
        </span>

        <!-- 已登录显示元素 -->
        <span id="userLoginElement" style="display: none;">
            <a href="personal_center.html">个人中心</a>
            <a href="myblog_list.html">个人博客</a>
            <a href="my_relation_following.html">我的关注/粉丝</a>
            <a href="private_message.html">聊天室</a>
            <a href="draft_list.html">我的草稿</a>
            <a href="blog_add.html">写博客</a>
            <a href="javascript:logout()">注销</a>
        </span>
    </div>

    <!-- 版心 -->
    <div class="container">
        <div class="container-left">
            <div class="card">
                <img id="avatar-img" src="img/avatar.png" class="avtar" alt="Avatar">
                <h3 id="author2"></h3>
                <span id="notLoginUser" class="isLogin">
                    <button class="button attention-button" user-id="0"
                        onclick="toggleFollow(this.getAttribute('user-id'))"> 关注 </button>
                </span>

                <span id="isLoginUser" class="isLogin">
                    <button class="button my-attention-button" onclick="toFollowingPage()">我的关注</button>
                    <button class="button my-fans-button" onclick="toFollowsPage()">我的粉丝</button>
                </span>

                <a id="url" href="#" target="_blank">gitee 地址</a>
                <div class="counter">
                    <span class="icon icon-counter"></span>
                    <span id="counter">文章数量：0</span>
                </div>
                <div class="reading-times">
                    <span class="icon icon-reading"></span>
                    <span id="reading-times">总访问量：9999</span>
                </div>
                <div class="comment-times">
                    <span class="icon icon-comment"></span>
                    <span id="comment-times">获得评论：2 次</span>
                </div>

            </div>
        </div>

        <!-- 右侧内容详情 -->
        <div id="artlist" class="container-right">

            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <!-- <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="desc">
                    从今天起, 我要认真敲代码. Lorem ipsum, dolor sit amet consectetur adipisicing elit. Cum distinctio ullam eum ut
                    veroab laborum numquam tenetur est in dolorum a sint, assumenda adipisci similique quaerat vel.
                    Facere,
                    et.
                </div>
                <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;
            </div> -->
        </div>
    </div>

    <script>
        // 当前用户id
        var uid = getParamByKey('uid');

        function getUserInfoByUId() {
            jQuery.ajax({
                url: "user/getuserinfobyuid",
                type: "POST",
                data: {
                    "uid": uid
                },
                success: function (res) {
                    if (res.code == 200 && res.data != null && res.data.id > 0) {
                        // 获取用户信息成功！
                        // 头像
                        if (res.data.photo != null && res.data.photo != '') {
                            jQuery("#avatar-img").attr("src", "/photo/" + res.data.photo);
                        } else {
                            jQuery("#avatar-img").attr("src", "/img/avatar.png");
                        }

                        // 用户名或昵称
                        if (res.data.nickname != null && res.data.nickname != 'undefined') {
                            jQuery("#author2").html(res.data.nickname);
                            jQuery("#pageTitle").html(res.data.nickname + "的博客");
                        } else {
                            jQuery("#author2").html(res.data.username);
                            jQuery("#pageTitle").html(res.data.username + "的博客");
                        }

                        // gitee地址
                        if (res.data.gitee != null && res.data.gitee != '') {
                            jQuery("#url").attr("href", res.data.gitee);
                        } else {
                            console.log("该作者未设置gitee地址！");
                        }

                        // 获取总访问量和评论总数
                        getTotalRCountAndCommentByUId(res.data.id);

                        // 是否是当前用户
                        isLoginUser(res.data.id);

                    } else {
                        alert("获取作者信息失败！" + res.msg);
                        location.href = "blog_list.html";
                    }
                }
            });
        }
        getUserInfoByUId();


        // 更新按钮状态的函数
        function updateButtonState(userId, isFollowed) {
            const button = document.querySelector(".attention-button");

            if (isFollowed) {
                button.textContent = "已关注";
            } else {
                button.textContent = "关注";
            }
        }

        // 点击关注按钮时触发的函数
        function toggleFollow(userId) {
            const button = document.querySelector(".attention-button");
            const isFollowed = button.textContent.trim() === "已关注";

            if (isFollowed) {
                // 执行取消关注逻辑，通过发送 AJAX 请求给服务器
                // 然后根据返回结果来更新按钮和状态
                cancelFollow(userId, button);
            } else {
                // 执行关注逻辑，可以通过发送 AJAX 请求给服务器
                // 然后根据返回结果来更新按钮和状态
                follow(userId, button);
            }
        }

        // 取消关注函数
        function cancelFollow(followingId, button) {
            console.log("cancelFollow: " + followingId);
            // 发送取消关注的 AJAX 请求
            jQuery.ajax({
                url: "follow/del_follow_relationship",
                type: "POST",
                data: {
                    "followingId": followingId
                },
                success: function (res) {
                    if (res.code == 200 && res.data == 1) {
                        // 取消关注成功 
                        // 更新按钮状态
                        updateButtonState(followingId, false); // 将按钮状态设置为未关注
                    } else {
                        alert("取消关注失败！" + res.msg);
                    }
                }
            });
        }

        // 关注函数
        function follow(followingId, button) {
            console.log("follow: " + followingId);
            // 发送关注的 AJAX 请求
            jQuery.ajax({
                url: "follow/add_follow_relationship",
                type: "POST",
                data: {
                    "followingId": followingId
                },
                success: function (res) {
                    if (res.code == 200 && res.data == 1) {
                        // 关注成功 
                        // 更新按钮状态
                        updateButtonState(followingId, true); // 将按钮状态设置为已关注
                    } else if (res.code == 200 && res.data == 0) {
                        alert("已经关注了该用户！");
                    } else {
                        alert("关注失败！" + res.msg);
                    }
                }
            });
        }



        //////////////////////////

        function isLoginUser(uid) {
            jQuery.ajax({
                url: "user/is_login_user",
                type: "POST",
                data: {
                    "uid": uid
                },
                success: function (res) {
                    if (res.code == 200) {
                        jQuery(".isLogin").show();
                        if (res.data == 1) {
                            // 当前文章属于登录的用户
                            jQuery("#isLoginUser").show();
                            jQuery("#notLoginUser").hide();
                        }
                        else if (res.data == -1) {
                            // 当前文章不属于登录的用户
                            jQuery("#isLoginUser").hide();
                            jQuery("#notLoginUser").show();
                            // 添加属性user-id
                            jQuery(".attention-button").attr("user-id", uid);

                            // 判断是否关注该用户，来更新按钮状况
                            isFollowing(uid);
                        } else if (res.data == 0) {
                            // 当前用户未登录
                            jQuery(".isLogin").hide();
                        }
                    } else {
                        console.log("出现错误！" + res.msg);
                        location.href = "blog_list.html";
                    }
                }
            });
        }

        function isFollowing(followingId) {
            console.log("isFollowing:" + followingId);
            jQuery.ajax({
                url: "follow/is_following",
                type: "POST",
                data: {
                    "followingId": followingId
                },
                success: function (res) {
                    if (res.code == 200 && res.data == true) {
                        // 已经关注
                        jQuery(".attention-button").html("已关注")
                    } else if (res.code == 200 && res.data == false) {
                        jQuery(".attention-button").html("关注")
                    } else {
                        alert("查询失败！" + res.msg);
                    }
                }
            });
        }


        function getAtrListByUId() {
            jQuery.ajax({
                url: "art/getlistbyuid",
                type: "POST",
                data: {
                    "uid": uid
                },
                success: function (res) {
                    if (res.code == 200 && res.data.length > 0) {

                        // 文章数量
                        jQuery("#counter").html("文章总数：" + res.data.length);

                        // for 拼接 div，最终放到 mylist
                        var arthtml = "";
                        for (var i = 0; i < res.data.length; i++) {
                            var art = res.data[i];
                            arthtml += '<div class="blog">';
                            arthtml += '<div class="title">' + art.title + '</div>';
                            arthtml += '<div class="date">' + art.createtime + '</div>';
                            arthtml += '<div class="desc">';
                            arthtml += art.content;
                            arthtml += '</div>';
                            arthtml += '<a href="blog_content.html?id=' + art.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                            arthtml += '</div>';
                        }
                        jQuery("#artlist").html(arthtml);
                    } else if (res.code == 200 && res.data.length == 0) {
                        // 暂无文章
                        jQuery("#artlist").html("<h2 style = 'margin-left:100px;margin-top:100px'>暂无文章！</h2>")
                    } else {
                        alert("获取文章列表失败！" + res.msg);
                        location.href = "blog_list.html";
                    }
                }
            });
        }

        getAtrListByUId();

        function performSearch() {
            var searchKey = jQuery("#search-input");
            if (searchKey.val().trim() == '') {
                alert("请先输入搜索关键字！");
                return;
            }

            console.log("搜索关键字：" + searchKey.val());
            var searchUrl = 'search_list.html?query=' + encodeURIComponent(searchKey.val());
            console.log("url：" + searchUrl);

            // 打开新窗口
            window.open(searchUrl, '_blank');
        }

        function toFollowingPage() {
            location.href = "my_relation_following.html";
        }

        function toFollowsPage() {
            location.href = "my_relation_follows.html";
        }
    </script>

</body>

</html>